#! /usr/bin/env python
#coding=utf-8
import wx
import wx.html as html
import Tms

def loadOtherButtons(self, item):
    """ Loads 'Other' buttons with info """
    if item in self.p.db.Calendar:
        date = self.p.db.Calendar[item]
        self.btnOtLtCleaning.SetLabel(date.get('LightCleaning', ''))
        self.btnOtHvCleaning.SetLabel(date.get('HeavyCleaning', ''))
    else:
        self.btnOtHvCleaning.SetLabel('')
        self.btnOtHvCleaning.SetLabel('')
    
def loadWtButtons(self, item):
    """ Loads 'WT' buttons with info """
    if item in self.p.db.Calendar:
        date = self.p.db.Calendar[item]
        self.btnWtReader.SetLabel(Tms.GetStudentsName(self, date.get('Wt Reader', '')))
    else:
        self.btnWtReader.SetLabel('')
        
    if item in self.p.db.SourceMaterial:
        date = self.p.db.SourceMaterial[item]
        self.cmbbxWtSong1.SetSelection(date.get('W Song1',0))
        self.cmbbxWtSong2.SetSelection(date.get('W Song2',0))
    else:
        self.cmbbxWtSong1.SetSelection(0)
        self.cmbbxWtSong2.SetSelection(0)
        
def loadPtButtons(self, item):
    """ Loads 'PT' buttons with info """
    if item in self.p.db.Calendar:
        date = self.p.db.Calendar[item]
        name = Tms.GetStudentsName(self, date.get('PT Chairman',''))
        if name != '':
            self.btnPtChairman.SetLabel(name)
        else:
            self.btnPtChairman.SetLabel('Click to Select Chairman')
        self.btnPtSpeaker.SetLabel(date.get('PT Speaker',''))
        self.btnPtCongregation.SetLabel(date.get('PT Congregation',''))
        self.btnPtTheme.SetLabel(date.get('PT Theme', ''))
        self.btnPtHospitality.SetLabel(date.get('PT Hospitality', ''))
        self.btnPtAway1Cong.SetLabel(date.get('Away1 Cong', ''))
        self.btnPtAway1Theme.SetLabel(date.get('Away1 Theme', ''))
        self.btnPtAway1Speaker.SetLabel(Tms.GetStudentsName(self, date.get('Away1 Speaker', '')))
        self.btnPtAway2Cong.SetLabel(date.get('Away2 Cong', ''))
        self.btnPtAway2Theme.SetLabel(date.get('Away2 Theme', ''))
        self.btnPtAway2Speaker.SetLabel(Tms.GetStudentsName(self, date.get('Away2 Speaker', '')))
        self.btnPtAway3Cong.SetLabel(date.get('Away3 Cong', ''))
        self.btnPtAway3Theme.SetLabel(date.get('Away3 Theme', ''))
        self.btnPtAway3Speaker.SetLabel(Tms.GetStudentsName(self, date.get('Away3 Speaker', '')))
    else:
        self.btnPtChairman.SetLabel('')
        self.btnPtSpeaker.SetLabel('')
        self.btnPtCongregation.SetLabel('')
        self.btnPtTheme.SetLabel('')
        self.btnPtHospitality.SetLabel('')
        self.btnPtAway1Cong.SetLabel('')
        self.btnPtAway1Theme.SetLabel('')
        self.btnPtAway1Speaker.SetLabel('')
        self.btnPtAway2Cong.SetLabel('')
        self.btnPtAway2Theme.SetLabel('')
        self.btnPtAway2Speaker.SetLabel('')
        self.btnPtAway3Cong.SetLabel('')
        self.btnPtAway3Theme.SetLabel('')
        self.btnPtAway3Speaker.SetLabel('')
    
def loadSmButtons(self, item):
    """ Load 'SM' buttons with info"""
    self.btnLoadSong(btn=self.cmbbxSmSong1, partID='SM Song1', Item=item)
    self.btnLoadSong(btn=self.cmbbxSmSong2, partID='SM Song2', Item=item)
    
    self.loadSourceMaterial(btn=self.btnSmPart1Source, partID='SM 1 Source', Item=item)
    self.loadSourceMaterial(btn=self.btnSmPart2Source, partID='SM 2 Source', Item=item)
    self.loadSourceMaterial(btn=self.btnSmPart3Source, partID='SM 3 Source', Item=item)
    self.loadSourceMaterial(btn=self.btnSmPart4Source, partID='SM 4 Source', Item=item)
    self.loadSourceMaterial(btn=self.btnSmPart5Source, partID='SM 5 Source', Item=item)
        
    self.loadBtnConductor(Item=item, btn=self.btnSmPart1Conductor, partID='SM Part1 ID')
    self.loadBtnConductor(Item=item, btn=self.btnSmPart2Conductor, partID='SM Part2 ID')
    self.loadBtnConductor(Item=item, btn=self.btnSmPart3Conductor, partID='SM Part3 ID')
    self.loadBtnConductor(Item=item, btn=self.btnSmPart4Conductor, partID='SM Part4 ID')
    self.loadBtnConductor(Item=item, btn=self.btnSmPart5Conductor, partID='SM Part5 ID')
    
def loadTmsButtons(self, item):
    """ Load 'TMS' tab with info for this date """
    
    #first assign how many schools to show
    schools = 1
    if item in self.p.db.Calendar:
        if (item in self.p.db.SourceMaterial) and ('Review' in self.p.db.SourceMaterial[item]) and (self.p.db.SourceMaterial[item]['Review']=='Y'):
            schools = 1
        else:
            schools = int(self.p.db.Calendar[item].get('#Schools',self.p.db.Options['#Schools']))
    else:
        schools = int(self.p.db.Options['#Schools'])
    
    if item in self.p.db.Calendar: #if this selected date is in calendar
        date = self.p.db.Calendar[item]
        self.cmbbxMeetingStatus.SetValue(date.get('Status','Unscheduled'))
        
        if (item in self.p.db.SourceMaterial) and ('Review' in self.p.db.SourceMaterial[item]) and self.p.db.SourceMaterial[item]['Review']=='Y':
            self.cmbbxNumSchools.SetValue('1')
            self.cmbbxNumSchools.Enable(False)
        else:
            self.cmbbxNumSchools.Enable(True)
            self.cmbbxNumSchools.SetValue(str(schools)) 
            
        self.ckbxCoVisit.SetValue(date.get('CO Visit',False))
    
        if item in self.p.db.SourceMaterial:
            self.sttxtHighlights.SetToolTipString(self.p.db.SourceMaterial[item].get('H Source',''))
        else:
            self.sttxtHighlights.SetToolTipString('')
        
        if date.has_key('H ID') and date['H ID'] !='' and (int(date['H ID']) in self.p.db.Students):
            self.btnHighlights.SetLabel(self.p.db.Students[int(date['H ID'])]['ScreenName'])
        else: 
            self.btnHighlights.SetLabel('Click to Assign Student')
    
        if item in self.p.db.SourceMaterial and 'Review' in self.p.db.SourceMaterial[item] and self.p.db.SourceMaterial[item]['Review']=='Y':
            review = True
            self.showStudentTalks(False)
    
            if date.has_key('Reader ID') and date['Reader ID'] != '' and (int(date['Reader ID']) in self.p.db.Students):
                self.btnTmsReader.SetLabel(self.p.db.Students[int(date['Reader ID'])]['ScreenName'])
            else: self.btnTmsReader.SetLabel('Click to Assign Student')
        else:
            self.showStudentTalks(True)
    
        if item in self.p.db.SourceMaterial:
            self.sttxtTalk1.SetToolTipString(self.p.db.SourceMaterial[item].get('1 Source',''))
        else:
            self.sttxtTalk1.SetToolTipString('')
    
        if date.has_key('1a ID') and date['1a ID'] != '' and (int(date['1a ID']) in self.p.db.Students): 
            self.btnTalk1.SetLabel(self.p.db.Students[int(date['1a ID'])].get('ScreenName','Click to Assign Student'))
        else: 
            self.btnTalk1.SetLabel('Click to Assign Student')  
            
        self.btnTalk1Study.SetLabel(str(date.get('1a StudyID','')))
        self.btnTalk1Study.SetToolTipString(Tms.studyNumber[date.get('1a StudyID',0)])
        
        if item in self.p.db.SourceMaterial:
            txt = self.p.db.SourceMaterial[item].get('2 Source','') + ': ' + self.p.db.SourceMaterial[item].get('2 Theme','')
            self.sttxtTalk2.SetToolTipString(txt)
        else:
            self.sttxtTalk2.SetToolTipString('')
        
        if date.has_key('2a ID') and date['2a ID'] != '': 
            self.btnTalk2.SetLabel(self.p.db.Students[int(date['2a ID'])].get('ScreenName','Click to Assign Student'))
        else: 
            self.btnTalk2.SetLabel('Click to Assign Student')  
    
        self.btnTalk2Study.SetLabel(str(date.get('2a StudyID','')))
        self.btnTalk2Study.SetToolTipString(Tms.studyNumber[date.get('2a StudyID',0)])
    
        if date.has_key('2a Assist ID') and date['2a Assist ID'] != '': 
            self.btnTalk2Assist.SetLabel(self.p.db.Students[int(date['2a Assist ID'])].get('ScreenName','Click to Assign Student'))
        else: self.btnTalk2Assist.SetLabel('Click to Assign Student')  
    
        if item in self.p.db.SourceMaterial and '3 Source' in self.p.db.SourceMaterial[item]:
            txt = self.p.db.SourceMaterial[item]['3 Source'] + ': ' + self.p.db.SourceMaterial[item]['3 Theme']
            self.sttxtTalk3.SetToolTipString(txt)
        else:
            self.sttxtTalk3.SetToolTipString('')
        
        if date.get('3a ID','') != '': 
            self.btnTalk3.SetLabel(self.p.db.Students[int(date['3a ID'])].get('ScreenName','Click to Assign Student'))
            if date.get('3a ID','Empty') in self.p.db.Students:
                if self.p.db.Students[date['3a ID']]['Sex']=='Male':
                    self.sttxtTalk3Assist.Show(False)
                    self.btnTalk3Assist.Show(False)
                else:
                    self.sttxtTalk3Assist.Show(True)
                    self.btnTalk3Assist.Show(True)
        else: 
            self.btnTalk3.SetLabel('Click to Assign Student')  
        
        self.btnTalk3Study.SetLabel(str(date.get('3a StudyID','')))
        self.btnTalk3Study.SetToolTipString(Tms.studyNumber[date.get('3a StudyID',0)])
    
        if date.has_key('3a Assist ID') and date['3a Assist ID'] != '' and int(date['3a Assist ID']) in self.p.db.Students: 
            self.btnTalk3Assist.SetLabel(self.p.db.Students[int(date['3a Assist ID'])].get('ScreenName','Click to Assign Student'))
        else: 
            self.btnTalk3Assist.SetLabel('Click to Assign Student')  
    
    else: #date is not in calendar, so fill with defaults
        self.showStudentTalks(True)
    
        self.cmbbxMeetingStatus.SetValue('Unscheduled')
        self.cmbbxNumSchools.Enable(True)
        self.cmbbxNumSchools.SetValue(str(self.p.db.Options['#Schools']))
        self.ckbxCoVisit.SetValue(False)
        self.btnHighlights.SetLabel('Click to Assign Student')
        self.radioSchoolAssignments.Show(True)
        
        self.sttxtTalk1.SetToolTipString('')
        self.btnTalk1.SetLabel('Click to Assign Student')
        self.btnTalk1Study.SetToolTipString('')
        
        self.btnTalk2.SetLabel('')
        self.btnTalk2Study.SetLabel('')
        self.btnTalk2Assist.SetLabel('Click to Assign Student')
        
        self.btnTalk3.SetLabel('Click to Assign Student')
        self.btnTalk3Study.SetLabel('')
        self.btnTalk3Assist.SetLabel('Click to Assign Student')
        
        self.sttxtHighlights.SetToolTipString('')
        self.sttxtTalk2.SetToolTipString('')
        self.sttxtTalk3.SetToolTipString('')
        
    if schools == 1:
        self.radioSchoolAssignments.ShowItem(1,False)
        self.radioSchoolAssignments.ShowItem(2,False)
    elif schools == 2:
        self.radioSchoolAssignments.ShowItem(1,True)
        self.radioSchoolAssignments.ShowItem(2,False)
    elif schools == 3:
        self.radioSchoolAssignments.ShowItem(1,True)
        self.radioSchoolAssignments.ShowItem(2,True)
        
    self.radioSchoolAssignments.SetStringSelection('1')
    
def loadCbsButtons(self, item):
    if item in self.p.db.Calendar:
        date = self.p.db.Calendar[item]
        name = Tms.GetStudentsName(self, date.get('CBS Conductor',''))
        if name != '':
            self.btnCbsConductor.SetLabel(name)
        else:
            self.btnCbsConductor.SetLabel('Click to Select Conductor')
    else:
        self.btnCbsConductor.SetLabel('Click to Select Conductor')
        
    if item in self.p.db.SourceMaterial:
        date = self.p.db.SourceMaterial[item]
        try:
            song = int(date.get('CBS Song', 0))
            if song == '':
                song = 0
        except:
            song = 0
        self.cmbbxCbsSong.SetSelection(song)
        source = date.get('CBS Source','Enter Source Material')
        if source == '': source = 'Enter Source Material'
        self.btnCbsSource.SetLabel(source)
    else:
        self.cmbbxCbsSong.SetSelection(0)
        self.btnCbsSource.SetLabel('Enter Source Material')

class enterResults(wx.Dialog):
    def __init__(self, parent, talk, school, date):
        self.p = parent
        wx.Dialog.__init__(self, parent, -1, 'Enter the results for this talk', 
            (299, 224), (490, 370), wx.DEFAULT_DIALOG_STYLE,)
        self.Bind(wx.EVT_CLOSE, self.On_Close)
        self.Center(wx.BOTH)

        self.date = date
        self.talk = talk
        if talk=='Highlights': school = 1
        self.school = school
        
        talks = {'Highlights': ['', 'H ID', 'H ID', 'H ID'],
            'TMS Reader': ['', 'Reader ID', 'Reader ID', 'Reader ID'],
            'Talk 1': ['', '1a ID', '1b ID', '1c ID'],
            'Talk 2': ['', '2a ID', '2b ID', '2c ID'],
            'Talk 2 Assist': ['', '2a Assist ID', '2b Assist ID', '2c Assist ID'],
            'Talk 3': ['', '3a ID', '3b ID', '3c ID'],
            'Talk 3 Assist': ['', '3a Assist ID', '3b Assist ID', '3c Assist ID']}
        studies = {'Talk 1': ['', '1a StudyID', '1b StudyID', '1c StudyID'],
            'Talk 2': ['', '2a StudyID', '2b StudyID', '2c StudyID'],
            'Talk 3': ['', '3a StudyID', '3b StudyID', '3c StudyID']}
            
        thisTalk = talks[talk][school]
        self.studentID = self.p.p.db.Calendar[date][thisTalk]
        
        self.sttxtTalk = wx.StaticText(self, pos=(10,12), label='Talk: ')
        self.txtTalk = wx.TextCtrl(self, pos=(40,10), size=(75,-1), value=talk, style=wx.TE_READONLY)
        self.sttxtSchool = wx.StaticText(self, pos=(130,12), label='School: ')
        self.txtSchool = wx.TextCtrl(self, pos=(170, 10), size=(15,-1), value=str(school),  style=wx.TE_READONLY)
        
        self.sttxtStudent = wx.StaticText(self, pos=(200,12), label='Student: ')
        self.txtStudent = wx.TextCtrl(self, pos=(250,10), size=(150,-1), value=Tms.GetStudentsName(self.p, self.studentID), style=wx.TE_READONLY)
        self.radioTalkGiven = wx.RadioBox(self, pos=(10, 42), label='Talk Given By Student?', choices=['Yes', 'No'])
        self.radioTalkGiven.Bind(wx.EVT_RADIOBOX, self.on_radioTalkGiven_click)
        self.sttxtSubstitute = wx.StaticText(self, pos=(170, 52), label='Substitute:')
        self.sttxtSubstitute.Show(False)
        self.cmbbxSubstitute = wx.ComboBox(self, pos=(235, 50), choices=Tms.loadStudentsList(self.p, 'Results'))
        self.cmbbxSubstitute.Show(False)
        
        if (talk == 'Talk 1') or (talk == 'Talk 2') or (talk == 'Talk 3'):
            self.sttxtStudy = wx.StaticText(self,pos=(10,90),label="Study:")
            self.cmbbxStudy = wx.ComboBox(self,pos=(10,105),size=(240,-1),choices=Tms.studyNumber)
            study = self.p.p.db.Calendar[date][studies[talk][school]]
            self.cmbbxStudy.SetSelection(study)
            self.sttxtStudyAssigned = wx.StaticText(self,pos=(255,90),label="Date Assigned:")
            self.dateStudyAssigned = wx.DatePickerCtrl(self, pos=(255,105), size=(105,-1), style=wx.DP_DROPDOWN|wx.DP_ALLOWNONE|wx.DP_SHOWCENTURY)
            self.dateStudyAssigned.SetValue(self.getDateStudyAssigned(study))
            self.sttxtStudyCompleted = wx.StaticText(self,pos=(365,90), label="Date Completed:")
            self.dateStudyCompleted = wx.DatePickerCtrl(self, pos=(365,105), size=(105,-1), style=wx.DP_DROPDOWN|wx.DP_ALLOWNONE|wx.DP_SHOWCENTURY)
            if (talk != 'Talk 1') and self.p.p.db.Students[self.studentID]['Sex']=='Female':
                self.sttxtSetting = wx.StaticText(self,pos=(10,130),label="Setting:")
                self.cmbbxSetting = wx.ComboBox(self,pos=(10,145),size=(350,-1),choices=Tms.settingNumber)
                self.sttxtSettingUsed = wx.StaticText(self,pos=(365,130), label="Date Used:")
                self.dateSettingUsed = wx.DatePickerCtrl(self, pos=(365,145), size=(105,-1), style=wx.DP_DROPDOWN|wx.DP_SHOWCENTURY)
        
        sttxtRemarks = wx.StaticText(self, pos=(10,170), label="Remarks:")
        self.txtRemarks = wx.TextCtrl(self, pos=(10,190), size=(460,100), style=wx.TE_MULTILINE)
        
        self.btnSave = wx.Button(self, pos=(10,305), label='Save')
        self.btnSave.Bind(wx.EVT_BUTTON, self.on_btnSave_select)
        self.btnCancel = wx.Button(self, pos=(90,305), label='Cancel')
        self.btnCancel.Bind(wx.EVT_BUTTON, self.on_btnCancel_select)
        
    def on_btnCancel_select(self, event=None):
        self.Close()
        event.Skip()
        
    def on_btnSave_select(self, event=None):
        sex = self.p.p.db.Students[self.studentID]['Sex']
        if self.studentID not in self.p.p.db.TalkHistory:
            self.p.p.db.TalkHistory[self.studentID] = []
        tHist = self.p.p.db.TalkHistory[self.studentID]
        talk = self.talk
        if self.talk == 'TMS Reader': talk = 'Reader'
        if self.talk[-6:] == 'Assist': talk = 'ASST'
        if (self.talk == 'Highlights') or (self.talk[-6:] == 'Assist') or (self.talk == 'TMS Reader'): 
            study = 0
        else:
            study = self.cmbbxStudy.GetSelection()
        if sex == ('Female') and ((self.talk=='Talk 2') or (self.talk=='Talk 3')):
            setting = self.cmbbxSetting.GetSelection()
        else:
            setting = 0
        if self.radioTalkGiven.GetStringSelection() == 'Yes':
            result = 'Completed'
        else:
            result = 'Cancelled'
            substitute = self.cmbbxSubstitute.GetStringSelection()
            if substitute != '':
                subID = Tms.GetStudentId(self.p, substitute)
                if subID not in self.p.p.db.TalkHistory:
                    self.p.p.db.TalkHistory[subID] = []
                self.p.p.db.TalkHistory[subID].append({'Date': Tms.convertCalendar2file(self.date),
                    'Talk':talk, 'Result': 'Fillin', 'Setting':setting, 'School':self.school,
                    'Remarks':self.txtRemarks.GetValue()})
        tHist.append({'Date': Tms.convertCalendar2file(self.date),
            'Talk': talk, 'Result':result, 'Remarks':self.txtRemarks.GetValue(),
            'Study':study, 'Setting': setting, 'School': self.school})
        if study != 0:
            tHist = self.p.p.db.StudyHistory
            if (self.studentID not in tHist) or (study not in tHist[self.studentID]):
                tHist[self.studentID] = {study:{}}
            self.p.p.db.StudyHistory[self.studentID][study]['Assigned'] = Tms.convertwxDate2fileFormat(self.dateStudyAssigned.GetValue())
            self.p.p.db.StudyHistory[self.studentID][study]['Completed'] = Tms.convertwxDate2fileFormat(self.dateStudyCompleted.GetValue())            
        if setting != 0:
            setH = self.p.p.db.SettingHistory
            if self.studentID not in setH:
                setH[self.studentID] = {}
            setH[self.studentID][setting] = Tms.convertwxDate2fileFormat(self.dateSettingUsed.GetValue())
        
        self.Close()
        event.Skip()
            
    def getDateStudyAssigned(self, study):
        #first check if study already exist in study history
        dat = self.p.p.db.StudyHistory
        if (self.studentID in dat) and (study in dat[self.studentID]) and (dat[self.studentID][study]['Completed'] ==''):
            Yr, Mo, Day = dat[self.studentID][study]['Assigned']
            date = wx.DateTimeFromDMY(int(Day), int(Mo)-1, int(Yr))
        else:
            date = Tms.convertGrid2wxDate(self.date)
        return date
    
    def on_radioTalkGiven_click(self, event=None):
        answer = self.radioTalkGiven.GetStringSelection()
        if answer == 'Yes':
            self.sttxtSubstitute.Show(False)
            self.cmbbxSubstitute.Show(False)
        else:
            self.sttxtSubstitute.Show(True)
            self.cmbbxSubstitute.Show(True)
            
    def On_Close(self, event=None):
        event.Skip()
    
class ScheduleFrame(wx.Frame):
    """The Schedule window for scheduling talks""" 
    def __init__(self, parent, *args, **kwargs):
        wx.Frame.__init__(self, parent, *args, **kwargs)
        self.p = parent
        #self.db = self.parent.db
        
        self.studentInfoText = '<b>Student Information:</b><br />'
        self.talks = ['H ID', '1a ID','2a ID','2a Assist ID','3a ID','3a Assist ID','1b ID','2b ID','2b Assist ID','3b ID','3b Assist ID', 'Reader ID']
        self.Bind(wx.EVT_CLOSE, self.on_Schedule_close)

        self.SchedulePanel = wx.Panel(self)
        
        self.sttxtSortType = wx.StaticText(self.SchedulePanel, pos=(7,7), label='Sort by:')
        self.cmbbxSortType = wx.ComboBox(self.SchedulePanel, pos=(5,22), choices=['Last Talk Given','A to Z'], style=wx.CB_READONLY)
        self.cmbbxSortType.Bind(wx.EVT_COMBOBOX, self.on_cmbbxSortType_select)
        self.radioSortSex = wx.RadioBox(self.SchedulePanel, pos=(115,0), label='Show:', choices=['Both','Male','Female'])
        self.radioSortSex.Bind(wx.EVT_RADIOBOX, self.on_radioSortSex_select)
        self.lstStudent = wx.ListBox(self.SchedulePanel, size=(150,495), pos=(0,48), choices=[], style=wx.LB_HSCROLL|wx.LB_NEEDED_SB|wx.LB_SINGLE)
        self.lstStudent.Bind(wx.EVT_LISTBOX, self.on_lstStudent_select)
        self.btnBack = wx.Button(self.SchedulePanel, label="<<<< Back", pos=(37,548))
        self.btnBack.Bind(wx.EVT_BUTTON, self.On_Back_btn)
        
        # create the student info box
        self.htmlStudentInfo = html.HtmlWindow(self.SchedulePanel, size=(635,185), pos=(155,48))
        self.htmlStudentInfo.AppendToPage(self.studentInfoText)

        # Calendar schedule items
        sttxtScheduleDates = wx.StaticText(self.SchedulePanel, pos=(170,243), label="TMS Calendar:")
        self.treeDates = wx.TreeCtrl(self.SchedulePanel, pos=(155,258), size=(120,310), style=wx.TR_HIDE_ROOT|wx.TR_SINGLE)
        self.treeDates.Bind(wx.EVT_TREE_SEL_CHANGED, self.on_treeDates_select)

        self.radioSchedule = wx.RadioBox(self.SchedulePanel, pos=(290, 240), label='Choose Meeting to Schedule', choices=['CBS','TMS','SM','PT','WT', 'Other'])
        self.radioSchedule.Bind(wx.EVT_RADIOBOX, self.on_radioSchedule_select)
        
        self.sttxtMeetingStatus = wx.StaticText(self.SchedulePanel, pos=(290, 293), label='Meeting Status:')
        self.cmbbxMeetingStatus = wx.ComboBox(self.SchedulePanel, pos=(370,290), choices=['Unscheduled', 'Scheduled', 'Concluded','Cancelled'], style=wx.CB_READONLY)
        self.cmbbxMeetingStatus.Bind(wx.EVT_COMBOBOX, self.on_cmbbxMeetingStatus_change)
        self.sttxtNumSchools = wx.StaticText(self.SchedulePanel, pos=(475, 293), label='# of Schools:')
        self.cmbbxNumSchools = wx.ComboBox(self.SchedulePanel, pos=(543, 290), choices=['1','2','3'], style=wx.CB_READONLY)
        self.cmbbxNumSchools.Bind(wx.EVT_COMBOBOX, self.on_cmbbxNumSchools_change)
        self.ckbxCoVisit = wx.CheckBox(self.SchedulePanel, pos=(590, 293), label="CO's Visit:", style=wx.ALIGN_RIGHT)
        self.ckbxCoVisit.Bind(wx.EVT_CHECKBOX, self.on_ckbxCoVisit_change)

        self.sttxtHighlights = wx.StaticText(self.SchedulePanel, pos=(290,323), label="Bible Highlights:")
        self.btnHighlights = wx.Button(self.SchedulePanel, pos=(430, 320), size=(125,-1), label='Click to Assign Student')
        self.btnHighlights.SetToolTipString('Click to select current Student')
        self.btnHighlights.Bind(wx.EVT_BUTTON, self.on_btnHighlights_select)
        self.btnHighlightsResults = wx.Button(self.SchedulePanel, pos=(710, 320), label='Results')
        self.btnHighlightsResults.Bind(wx.EVT_BUTTON, self.on_btnResults_click)
        self.btnHighlightsResults.Show(False)

        self.sttxtTmsReader = wx.StaticText(self.SchedulePanel, pos=(290, 348), label='TMS School Review Reader:')
        self.btnTmsReader = wx.Button(self.SchedulePanel, pos=(430, 345), size=(125,-1), label='Click to Assign Student')
        self.btnTmsReader.SetToolTipString('Click to select current Student')
        self.btnTmsReader.Bind(wx.EVT_BUTTON, self.on_btnTmsReader_select)
        self.btnTmsReaderResults = wx.Button(self.SchedulePanel, pos=(710, 345), label='Results')
        self.btnTmsReaderResults.Bind(wx.EVT_BUTTON, self.on_btnResults_click)
        self.btnTmsReaderResults.Show(False)

        self.radioSchoolAssignments = wx.RadioBox(self.SchedulePanel, pos=(290, 345), label='Assignments for School:', choices=['1','2','3'])
        self.radioSchoolAssignments.Bind(wx.EVT_RADIOBOX, self.on_radioSchoolAssignments_select)

        self.sttxtTalk1 = wx.StaticText(self.SchedulePanel, pos=(290,420), label='Talk 1:')
        self.btnTalk1 = wx.Button(self.SchedulePanel, pos=(350,417), size=(125,-1), label='Click to Assign Student')
        self.btnTalk1.SetToolTipString('Click to select current Student')
        self.btnTalk1.Bind(wx.EVT_BUTTON, self.on_btnTalk1_select)
        self.sttxtTalk1Study = wx.StaticText(self.SchedulePanel, pos=(485, 420), label='Study:')
        self.btnTalk1Study = wx.Button(self.SchedulePanel, pos=(525, 417), size=(35,-1), label='')
        self.btnTalk1Study.Bind(wx.EVT_BUTTON, self.on_btnTalk1Study_select)
        self.btnTalk1Results = wx.Button(self.SchedulePanel, pos=(710, 417), label='Results')
        self.btnTalk1Results.Bind(wx.EVT_BUTTON, self.on_btnResults_click)
        self.btnTalk1Results.Show(False)

        self.sttxtTalk2 = wx.StaticText(self.SchedulePanel, pos=(290,455), label='Talk 2:')
        self.btnTalk2 = wx.Button(self.SchedulePanel, pos=(350,452), size=(125,-1), label='Click to Assign Student')
        self.btnTalk2.SetToolTipString('Click to select current Student')
        self.btnTalk2.Bind(wx.EVT_BUTTON, self.on_btnTalk2_select)
        self.sttxtTalk2Study = wx.StaticText(self.SchedulePanel, pos=(485, 455), label='Study:')
        self.btnTalk2Study = wx.Button(self.SchedulePanel, pos=(525, 452), size=(35,-1), label='')
        self.btnTalk2Study.Bind(wx.EVT_BUTTON, self.on_btnTalk2Study_select)
        self.btnTalk2Results = wx.Button(self.SchedulePanel, pos=(710, 452), label='Results')
        self.btnTalk2Results.Bind(wx.EVT_BUTTON, self.on_btnResults_click)
        self.btnTalk2Results.Show(False)

        self.sttxtTalk2Assist = wx.StaticText(self.SchedulePanel, pos=(290,480), label='2 Assistant:')
        self.btnTalk2Assist = wx.Button(self.SchedulePanel, pos=(350,477), size=(125,-1), label='Click to Assign Student')
        self.btnTalk2Assist.Bind(wx.EVT_BUTTON, self.on_btnTalk2Assist_select)
        self.btnTalk2AssistResults = wx.Button(self.SchedulePanel, pos=(710, 477), label='Results')
        self.btnTalk2AssistResults.Bind(wx.EVT_BUTTON, self.on_btnResults_click)
        self.btnTalk2AssistResults.Show(False)
        
        self.sttxtTalk3 = wx.StaticText(self.SchedulePanel, pos=(290,515), label='Talk 3:')
        self.btnTalk3 = wx.Button(self.SchedulePanel, pos=(350,512), size=(125,-1), label='Click to Assign Student')
        self.btnTalk3.Bind(wx.EVT_BUTTON, self.on_btnTalk3_select)
        self.sttxtTalk3Study = wx.StaticText(self.SchedulePanel, pos=(485,515), label='Study:')
        self.btnTalk3Study = wx.Button(self.SchedulePanel, pos=(525, 512), size=(35,-1), label='')
        self.btnTalk3Study.Bind(wx.EVT_BUTTON, self.on_btnTalk3Study_select)
        self.btnTalk3Results = wx.Button(self.SchedulePanel, pos=(710, 512), label='Results')
        self.btnTalk3Results.Bind(wx.EVT_BUTTON, self.on_btnResults_click)
        self.btnTalk3Results.Show(False)
        
        self.sttxtTalk3Assist = wx.StaticText(self.SchedulePanel, pos=(290,540), label='3 Assistant:')
        self.btnTalk3Assist = wx.Button(self.SchedulePanel, pos=(350,537), size=(125,-1), label='Click to Assign Student')
        self.btnTalk3Assist.Bind(wx.EVT_BUTTON, self.on_btnTalk3Assist_select)
        self.btnTalk3AssistResults = wx.Button(self.SchedulePanel, pos=(710, 537), label='Results')
        self.btnTalk3AssistResults.Bind(wx.EVT_BUTTON, self.on_btnResults_click)
        self.btnTalk3AssistResults.Show(False)
        
        # CBS controls
        self.sttxtCbsConductor = wx.StaticText(self.SchedulePanel, pos=(290,293), label='Conductor:')
        self.btnCbsConductor = wx.Button(self.SchedulePanel, pos=(390, 290), label='Click to Assign Conductor')
        self.btnCbsConductor.Bind(wx.EVT_BUTTON, self.on_btnCbsConductor_select)
        self.sttxtCbsSong = wx.StaticText(self.SchedulePanel, pos=(290, 323), label='Song:')
        self.cmbbxCbsSong = wx.ComboBox(self.SchedulePanel, pos = (390, 323), choices = Tms.songNumber)
        self.cmbbxCbsSong.Bind(wx.EVT_COMBOBOX, self.on_cmbbxCbsSong_select)
        self.sttxtCbsSource = wx.StaticText(self.SchedulePanel, pos=(290, 353), label='Source:')
        self.btnCbsSource = wx.Button(self.SchedulePanel, pos=(390, 350), label='Enter Source Material')
        self.btnCbsSource.Bind(wx.EVT_BUTTON, self.on_btnCbsSource_select)
        
        # SM Controls
        self.sttxtSmSong1 = wx.StaticText(self.SchedulePanel, pos=(290, 293), label='Opening Song:')
        self.cmbbxSmSong1 = wx.ComboBox(self.SchedulePanel, pos=(390, 290), choices=Tms.songNumber)
        self.cmbbxSmSong1.Bind(wx.EVT_COMBOBOX, self.on_cmbbxSmSong1_select)
        self.sttxtSmConductor = wx.StaticText(self.SchedulePanel, pos=(390, 323), label='Conductor:')
        self.sttxtSmSource = wx.StaticText(self.SchedulePanel, pos=(595, 323), label='Source Material:')

        self.sttxtSmPart1 = wx.StaticText(self.SchedulePanel, pos=(290, 348), label='Part 1:')
        self.btnSmPart1Conductor = wx.Button(self.SchedulePanel, pos=(340, 345), size=(160, -1), label='Click to Assign Conductor')
        self.btnSmPart1Conductor.Bind(wx.EVT_BUTTON, self.on_btnSmPart1Conductor_select)
        self.btnSmPart1Source = wx.Button(self.SchedulePanel, pos=(530, 345), size=(220, -1), label='Enter Source Material')
        self.btnSmPart1Source.Bind(wx.EVT_BUTTON, self.on_btnSmPart1Source_select)

        self.sttxtSmPart2 = wx.StaticText(self.SchedulePanel, pos=(290, 378), label='Part 2:')
        self.btnSmPart2Conductor = wx.Button(self.SchedulePanel, pos=(340, 375), size=(160, -1), label='Click to Assign Conductor')
        self.btnSmPart2Conductor.Bind(wx.EVT_BUTTON, self.on_btnSmPart2Conductor_select)
        self.btnSmPart2Source = wx.Button(self.SchedulePanel, pos=(530, 375), size=(220, -1), label='Enter Source Material')
        self.btnSmPart2Source.Bind(wx.EVT_BUTTON, self.on_btnSmPart2Source_select)
        
        self.sttxtSmPart3 = wx.StaticText(self.SchedulePanel, pos=(290, 408), label='Part 3:')
        self.btnSmPart3Conductor = wx.Button(self.SchedulePanel, pos=(340, 405), size=(160, -1), label='Click to Assign Conductor')
        self.btnSmPart3Conductor.Bind(wx.EVT_BUTTON, self.on_btnSmPart3Conductor_select)
        self.btnSmPart3Source = wx.Button(self.SchedulePanel, pos=(530, 405), size=(220, -1), label='Enter Source Material')
        self.btnSmPart3Source.Bind(wx.EVT_BUTTON, self.on_btnSmPart3Source_select)
        
        self.sttxtSmPart4 = wx.StaticText(self.SchedulePanel, pos=(290, 438), label='Part 4:')
        self.btnSmPart4Conductor = wx.Button(self.SchedulePanel, pos=(340, 435), size=(160, -1), label='Click to Assign Conductor')
        self.btnSmPart4Conductor.Bind(wx.EVT_BUTTON, self.on_btnSmPart4Conductor_select)
        self.btnSmPart4Source = wx.Button(self.SchedulePanel, pos=(530, 435), size=(220, -1), label='Enter Source Material')
        self.btnSmPart4Source.Bind(wx.EVT_BUTTON, self.on_btnSmPart4Source_select)

        self.sttxtSmPart5 = wx.StaticText(self.SchedulePanel, pos=(290, 468), label='Part 5:')
        self.btnSmPart5Conductor = wx.Button(self.SchedulePanel, pos=(340, 465), size=(160, -1), label='Click to Assign Conductor')
        self.btnSmPart5Conductor.Bind(wx.EVT_BUTTON, self.on_btnSmPart5Conductor_select)
        self.btnSmPart5Source = wx.Button(self.SchedulePanel, pos=(530, 465), size=(220, -1), label='Enter Source Material')
        self.btnSmPart5Source.Bind(wx.EVT_BUTTON, self.on_btnSmPart5Source_select)

        self.sttxtSmSong2 = wx.StaticText(self.SchedulePanel, pos=(290, 500), label='Closing Song:')
        self.cmbbxSmSong2 = wx.ComboBox(self.SchedulePanel, pos=(390, 500), choices=Tms.songNumber)
        self.cmbbxSmSong2.Bind(wx.EVT_COMBOBOX, self.on_cmbbxSmSong2_select)

        # PT Controls
        self.sttxtPtChairman = wx.StaticText(self.SchedulePanel, pos=(290, 293), label='Chairman:')
        self.btnPtChairman = wx.Button(self.SchedulePanel, pos=(390, 290), label='Click to Assign Chairman', style=wx.BU_LEFT)
        self.btnPtChairman.Bind(wx.EVT_BUTTON, self.on_btnPtChairman_select)
        self.sttxtPtSpeaker = wx.StaticText(self.SchedulePanel, pos=(290, 322), label='Speaker:')
        self.btnPtSpeaker = wx.Button(self.SchedulePanel, pos=(390, 318), size=(150,-1), label='', style=wx.BU_LEFT)
        self.btnPtSpeaker.SetToolTipString('Click to Enter Speaker')
        self.btnPtSpeaker.Bind(wx.EVT_BUTTON, self.on_btnPtSpeaker_click)
        self.sttxtPtCongregation = wx.StaticText(self.SchedulePanel, pos=(290, 347), label='Congregation:')
        self.btnPtCongregation = wx.Button(self.SchedulePanel, pos=(390, 345), size=(220, -1), style=wx.BU_LEFT)
        self.btnPtCongregation.SetToolTipString('Enter the Visiting Speakers Congregation')
        self.btnPtCongregation.Bind(wx.EVT_BUTTON, self.on_btnPtCongregation_click)
        self.sttxtPtTheme = wx.StaticText(self.SchedulePanel, pos=(290, 375), label='Talk Theme:')
        self.btnPtTheme = wx.Button(self.SchedulePanel, pos=(390, 372), size=(320, -1), style=wx.BU_LEFT)
        self.btnPtTheme.Bind(wx.EVT_BUTTON, self.on_btnPtTheme_click)
        self.sttxtPtHospitality = wx.StaticText(self.SchedulePanel, pos=(290, 402), label='Hospitality:')
        self.btnPtHospitality = wx.Button(self.SchedulePanel, pos=(390, 399), size=(220, -1), style=wx.BU_LEFT)
        self.btnPtHospitality.Bind(wx.EVT_BUTTON, self.on_btnPtHospitality_click)
        self.sttxtPtAwaySpeaker = wx.StaticText(self.SchedulePanel, pos=(310, 427), label='Away Speaker:')
        self.btnPtAway1Speaker = wx.Button(self.SchedulePanel, pos=(280, 447), size=(130, -1), style=wx.BU_LEFT)
        self.btnPtAway1Speaker.Bind(wx.EVT_BUTTON, self.on_btnPtAway1Speaker_click)
        self.sttxtPtAwayCong = wx.StaticText(self.SchedulePanel, pos=(425, 427), label='Away Congregation:')
        self.btnPtAway1Cong = wx.Button(self.SchedulePanel, pos=(415, 447), size=(130, -1), style=wx.BU_LEFT)
        self.btnPtAway1Cong.Bind(wx.EVT_BUTTON, self.on_btnPtAway1Cong_click)
        self.sttxtPtAwayTheme = wx.StaticText(self.SchedulePanel, pos=(560, 427), label='Away Theme:')
        self.btnPtAway1Theme = wx.Button(self.SchedulePanel, pos=(550, 447), size=(235, -1), style=wx.BU_LEFT)
        self.btnPtAway1Theme.Bind(wx.EVT_BUTTON, self.on_btnPtAway1Theme_click)
        self.btnPtAway2Speaker = wx.Button(self.SchedulePanel, pos=(280, 472), size=(130, -1), style=wx.BU_LEFT)
        self.btnPtAway2Speaker.Bind(wx.EVT_BUTTON, self.on_btnPtAway2Speaker_click)
        self.btnPtAway2Cong = wx.Button(self.SchedulePanel, pos=(415, 472), size=(130, -1), style=wx.BU_LEFT)
        self.btnPtAway2Cong.Bind(wx.EVT_BUTTON, self.on_btnPtAway2Cong_click)
        self.btnPtAway2Theme = wx.Button(self.SchedulePanel, pos=(550, 472), size=(235, -1), style=wx.BU_LEFT)
        self.btnPtAway2Theme.Bind(wx.EVT_BUTTON, self.on_btnPtAway2Theme_click)
        self.btnPtAway3Speaker = wx.Button(self.SchedulePanel, pos=(280, 497), size=(130, -1), style=wx.BU_LEFT)
        self.btnPtAway3Speaker.Bind(wx.EVT_BUTTON, self.on_btnPtAway3Speaker_click)
        self.btnPtAway3Cong = wx.Button(self.SchedulePanel, pos=(415, 497), size=(130, -1), style=wx.BU_LEFT)
        self.btnPtAway3Cong.Bind(wx.EVT_BUTTON, self.on_btnPtAway3Cong_click)
        self.btnPtAway3Theme = wx.Button(self.SchedulePanel, pos=(550, 497), size=(235, -1), style=wx.BU_LEFT)
        self.btnPtAway3Theme.Bind(wx.EVT_BUTTON, self.on_btnPtAway3Theme_click)
        
        # WT Controls
        self.sttxtWtSong1 = wx.StaticText(self.SchedulePanel, pos=(290, 293), label='Opening Song:')
        self.cmbbxWtSong1 = wx.ComboBox(self.SchedulePanel, pos=(390, 290), choices=Tms.songNumber)
        self.cmbbxWtSong1.Bind(wx.EVT_COMBOBOX, self.on_cmbbxWtSong1_select)
        
        self.sttxtWtReader = wx.StaticText(self.SchedulePanel, pos=(290, 323), label='Watchtower Reader:')
        self.btnWtReader = wx.Button(self.SchedulePanel, pos=(395, 320), size=(130, -1), style=wx.BU_LEFT)
        self.btnWtReader.Bind(wx.EVT_BUTTON, self.on_btnWtReader_click)
        
        self.sttxtWtSong2 = wx.StaticText(self.SchedulePanel, pos=(290, 363), label='Closing Song:')
        self.cmbbxWtSong2 = wx.ComboBox(self.SchedulePanel, pos=(390, 360), choices=Tms.songNumber)
        self.cmbbxWtSong2.Bind(wx.EVT_COMBOBOX, self.on_cmbbxWtSong2_select)
        
        # Other Controls
        self.sttxtOtLtCleaning = wx.StaticText(self.SchedulePanel, pos=(290, 293), label='Light Cleaning: ')
        self.btnOtLtCleaning = wx.Button(self.SchedulePanel, pos=(395, 290), size=(200, -1), style=wx.BU_LEFT)
        self.btnOtLtCleaning.Bind(wx.EVT_BUTTON, self.on_btnOtLtCleaning_click)
        self.sttxtOtHvCleaning = wx.StaticText(self.SchedulePanel, pos=(290, 318), label='Heavy Cleaning: ')
        self.btnOtHvCleaning = wx.Button(self.SchedulePanel, pos=(395, 320), size=(200, -1), style=wx.BU_LEFT)
        self.btnOtHvCleaning.Bind(wx.EVT_BUTTON, self.on_btnOtHvCleaning_click)
        
        self.Center(wx.BOTH)
        self.cmbbxSortType.SetStringSelection('Last Talk Given')
        self.radioSchedule.SetStringSelection('TMS')
        Tms.loadStudentsList(self, 'Schedule')
        self.loadScheduleWindow()
        self.on_lstStudent_select()
        self.on_treeDates_select()
        self.on_radioSchedule_select()
        self.on_cmbbxMeetingStatus_change()
        
    def on_btnResults_click(self, event=None):
        school = int(self.radioSchoolAssignments.GetStringSelection())        
        talks = {self.btnHighlightsResults:'Highlights', self.btnTmsReaderResults: 'TMS Reader',
            self.btnTalk1Results: 'Talk 1', self.btnTalk2Results: 'Talk 2', 
            self.btnTalk2AssistResults: 'Talk 2 Assist', self.btnTalk3Results: 'Talk 3',
            self.btnTalk3AssistResults: 'Talk 3 Assist'}
        win = event.GetEventObject()
        talk = talks[win]
        date = self.treeDates.GetItemText(self.treeDates.GetSelection())
        
        Dlg = enterResults(self, talk, school, date)
        try:
            Dlg.ShowModal()
        finally:
            Dlg.Destroy()
        
        
    def on_btnOtHvCleaning_click(self, event=None):
        Tms.onEnterPanelInfo(self, 'HeavyCleaning', self.btnOtHvCleaning, 'Enter the Group with Heavy Cleaning:', event)
        
    def on_btnOtLtCleaning_click(self, event=None):
        Tms.onEnterPanelInfo(self, 'LightCleaning', self.btnOtLtCleaning, 'Enter the Group with Light Cleaning:', event)
        
    def on_btnWtReader_click(self, event=None):
        self.selectStoreName(self.btnWtReader, 'Wt Reader', event)
        
    def on_btnPtAway3Theme_click(self, event=None):
        Tms.onEnterPanelInfo(self, 'Away3 Theme', self.btnPtAway3Theme, 'Enter Theme of Away Talk:', event)
        
    def on_btnPtAway3Cong_click(self, event=None):
        Tms.onEnterPanelInfo(self, 'Away3 Cong', self.btnPtAway3Cong, 'Enter the Congregation he is giving a talk in', event)
        
    def on_btnPtAway3Speaker_click(self, event=None):
        self.selectStoreName(self.btnPtAway3Speaker, 'Away3 Speaker', event)
        
    def on_btnPtAway2Theme_click(self, event=None):
        Tms.onEnterPanelInfo(self, 'Away2 Theme', self.btnPtAway2Theme, 'Enter Theme of Away Talk:', event)
        
    def on_btnPtAway2Cong_click(self, event=None):
        Tms.onEnterPanelInfo(self, 'Away2 Cong', self.btnPtAway2Cong, 'Enter the Congregation he is giving a talk in', event)
        
    def on_btnPtAway2Speaker_click(self, event=None):
        self.selectStoreName(self.btnPtAway2Speaker, 'Away2 Speaker', event)
        
    def on_btnPtAway1Theme_click(self, event=None):
        Tms.onEnterPanelInfo(self, 'Away1 Theme', self.btnPtAway1Theme, 'Enter Theme of Away Talk:', event)
        
    def on_btnPtAway1Cong_click(self, event=None):
        Tms.onEnterPanelInfo(self, 'Away1 Cong', self.btnPtAway1Cong, 'Enter the Congregation he is giving a talk in', event)
        
    def on_btnPtAway1Speaker_click(self, event=None):
        self.selectStoreName(self.btnPtAway1Speaker, 'Away1 Speaker', event)
        
    def on_btnPtHospitality_click(self, event=None):
        Tms.onEnterPanelInfo(self, 'PT Hospitality', self.btnPtHospitality, 'Enter the Bookstudy responsible for hospitality:', event)
        
    def on_btnPtTheme_click(self, event=None):
        Tms.onEnterPanelInfo(self, 'PT Theme', self.btnPtTheme, 'Enter the Title of the Public Talk', event)
        
    def on_btnPtCongregation_click(self, event=None):
        Tms.onEnterPanelInfo(self, 'PT Congregation', self.btnPtCongregation, 'Enter the Visiting Speakers Congregation:', event)        
        
    def on_btnPtSpeaker_click(self, event=None):
        Tms.onEnterPanelInfo(self, 'PT Speaker', self.btnPtSpeaker, 'Enter the Visiting Speakers Name:', event)        
        
    def selectStoreName(self, btn, partID, event):
        date = self.treeDates.GetItemText(self.treeDates.GetSelection())
        student = self.lstStudent.GetStringSelection()
        id = Tms.GetStudentId(self, student)
        if student != '':
            btn.SetLabel(student)
        else:
            btn.SetLabel('')
            
        if date in self.p.db.Calendar:
            self.p.db.Calendar[date][partID] = int(id)
        else:
            self.p.db.Calendar[date]={partID:int(id)}
        if event != None: event.Skip()
        
    def on_btnPtChairman_select(self, event=None):
        self.selectStoreName(self.btnPtChairman, 'PT Chairman', event)

    def on_btnSmPart5Source_select(self, event=None):
        self.btnSmSourceSelected(btn=self.btnSmPart5Source, SourceID='SM 5 Source', Event=event)
        
    def on_btnSmPart5Conductor_select(self, event=None):
        self.btnSmConductorSelected(btn=self.btnSmPart5Conductor, partID = 'SM Part5 ID', Event=event)
        
    def on_btnSmPart4Source_select(self, event=None):
        self.btnSmSourceSelected(btn=self.btnSmPart4Source, SourceID='SM 4 Source', Event=event)
        
    def on_btnSmPart4Conductor_select(self, event=None):
        self.btnSmConductorSelected(btn=self.btnSmPart4Conductor, partID = 'SM Part4 ID', Event=event)
        
    def on_btnSmPart3Source_select(self, event=None):
        self.btnSmSourceSelected(btn=self.btnSmPart3Source, SourceID='SM 3 Source', Event=event)
        
    def on_btnSmPart3Conductor_select(self, event=None):
        self.btnSmConductorSelected(btn=self.btnSmPart3Conductor, partID = 'SM Part3 ID', Event=event)
        
    def on_btnSmPart2Source_select(self, event=None):
        self.btnSmSourceSelected(btn=self.btnSmPart2Source, SourceID='SM 2 Source', Event=event)
        
    def on_btnSmPart2Conductor_select(self, event=None):
        self.btnSmConductorSelected(btn=self.btnSmPart2Conductor, partID = 'SM Part2 ID', Event=event)
        
    def on_btnSmPart1Source_select(self, event=None):
        self.btnSmSourceSelected(btn=self.btnSmPart1Source, SourceID='SM 1 Source', Event=event)
        
    def on_btnSmPart1Conductor_select(self, event=None):
        self.btnSmConductorSelected(btn=self.btnSmPart1Conductor, partID = 'SM Part1 ID', Event=event)
    
    def btnSmSourceSelected(self, btn, SourceID, Event):
        date = self.treeDates.GetItemText(self.treeDates.GetSelection())
        source = ''
        if date in self.p.db.SourceMaterial:
            source = self.p.db.SourceMaterial[date].get(SourceID,'')
        Dlg = Tms.enterInfo(self, oldSource=source, title='Enter Service Meeting Source')
        try:
            Dlg.ShowModal()
        finally:
            source= Dlg.source
            Dlg.Destroy()
            
        if date in self.p.db.SourceMaterial:
            self.p.db.SourceMaterial[date][SourceID] = source
        else:
            self.p.db.SourceMaterial[date] = {SourceID: source}
        if source == '': source = 'Enter Source Material'
        btn.SetLabel(source)
        if Event != None: Event.Skip()        
        
    def btnSmConductorSelected(self, btn, partID, Event):
        date = self.treeDates.GetItemText(self.treeDates.GetSelection())
        student = self.lstStudent.GetStringSelection()
        id = Tms.GetStudentId(self, student)
        if student != '':
            btn.SetLabel(student)
        else:
            btn.SetLabel('Click to Select Conductor')
            
        if date in self.p.db.Calendar:
            self.p.db.Calendar[date][partID] = int(id)
        else:
            self.p.db.Calendar[date]={partID:int(id)}
        if Event != None: Event.Skip()        
            
    def btnSongSelect(self, btn, partID, event):
        song = btn.GetSelection()
        date = self.treeDates.GetItemText(self.treeDates.GetSelection())
        if date in self.p.db.SourceMaterial:
            self.p.db.SourceMaterial[date][partID] = song
        else:
            self.p.db.SourceMaterial[date] = {partID: song}
        if event != None: event.Skip()
        
    def on_cmbbxSmSong2_select(self, Event=None):
        self.btnSongSelect(btn=self.cmbbxSmSong2, partID='SM Song2', event=Event)
        
    def on_cmbbxSmSong1_select(self, Event=None):
        self.btnSongSelect(btn=self.cmbbxSmSong1, partID='SM Song1', event=Event)
        
    def on_btnCbsSource_select(self, event=None):
        date = self.treeDates.GetItemText(self.treeDates.GetSelection())
        source = ''
        if date in self.p.db.SourceMaterial:
            source = self.p.db.SourceMaterial[date].get('CBS Source','')
        Dlg = Tms.enterInfo(self, oldSource=source, title='Enter CBS Source:')
        try:
            Dlg.ShowModal()
        finally:
            source= Dlg.source
            Dlg.Destroy()
        if date in self.p.db.SourceMaterial:
            if 'CBS Source' in self.p.db.SourceMaterial[date]:
                self.p.db.SourceMaterial[date]['CBS Source'] = source
            else:
                self.p.db.SourceMaterial[date]= {'CBS Source': source}
        else:
            self.p.db.SourceMaterial[date] = {'CBS Source': source}
        if source == '': source = 'Enter Source Material'
        self.btnCbsSource.SetLabel(source)
        if event != None: event.Skip()
    
    def on_cmbbxWtSong1_select(self, Event=None):
        self.btnSongSelect(btn=self.cmbbxWtSong1, partID='W Song1', event=Event)
        
    def on_cmbbxWtSong2_select(self, Event=None):
        self.btnSongSelect(btn=self.cmbbxWtSong2, partID='W Song2', event=Event)
        
    def on_cmbbxCbsSong_select(self, Event=None):
        self.btnSongSelect(btn=self.cmbbxCbsSong, partID='CBS Song', event=Event)
        
    def on_btnCbsConductor_select(self, event=None):
        date = self.treeDates.GetItemText(self.treeDates.GetSelection())
        student = self.lstStudent.GetStringSelection()
        id = Tms.GetStudentId(self, student)
        if student != '':
            self.btnCbsConductor.SetLabel(student)
        else:
            self.btnCbsConductor.SetLabel('Click to Select Conductor')
            
        if date in self.p.db.Calendar:
            self.p.db.Calendar[date]['CBS Conductor'] = int(id)
        else:
            self.p.db.Calendar[date]={'CBS Conductor':int(id)}
        event.Skip()
        
    def on_radioSortSex_select(self, event=None):
        Tms.loadStudentsList(self, 'Schedule')
        self.on_lstStudent_select()
        event.Skip()
        
    def on_cmbbxSortType_select(self, event=None):
        Tms.loadStudentsList(self, 'Schedule')
        if event != None: event.Skip()
        
    def on_radioSchedule_select(self, event=None):
        meeting = self.radioSchedule.GetStringSelection()
        allButtons = [{'CBS':[self.sttxtCbsConductor, self.btnCbsConductor, self.sttxtCbsSong,
            self.cmbbxCbsSong, self.sttxtCbsSource, self.btnCbsSource]},
            {'TMS':[self.sttxtMeetingStatus, self.cmbbxMeetingStatus, self.sttxtNumSchools,
            self.cmbbxNumSchools, self.ckbxCoVisit, self.sttxtHighlights, self.btnHighlights,
            self.sttxtTmsReader, self.btnTmsReader, self.radioSchoolAssignments, self.sttxtTalk1,
            self.btnTalk1, self.sttxtTalk1Study, self.btnTalk1Study, self.sttxtTalk2, self.btnTalk2,
            self.sttxtTalk2Study, self.btnTalk2Study, self.sttxtTalk2Assist, self.btnTalk2Assist,
            self.sttxtTalk3, self.btnTalk3, self.sttxtTalk3Study, self.btnTalk3Study, self.sttxtTalk3Assist,
            self.btnTalk3Assist, self.btnHighlightsResults, self.btnTalk1Results,
            self.btnTalk2Results, self.btnTalk2AssistResults, self.btnTalk3Results,
            self.btnTalk3AssistResults]},
            {'SM':[self.sttxtSmSong1, self.cmbbxSmSong1, self.sttxtSmConductor,self.sttxtSmPart1,
            self.btnSmPart1Conductor, self.sttxtSmSource, self.btnSmPart1Source, self.sttxtSmPart2,
            self.btnSmPart2Conductor, self.btnSmPart2Source, self.btnSmPart3Conductor, 
            self.btnSmPart3Source, self.sttxtSmPart3, self.sttxtSmPart4, self.sttxtSmPart5,
            self.btnSmPart4Conductor, self.btnSmPart5Conductor, self.btnSmPart4Source,
            self.btnSmPart5Source, self.sttxtSmSong2, self.cmbbxSmSong2]},
            {'PT':[self.sttxtPtChairman, self.btnPtChairman, self.sttxtPtSpeaker, self.btnPtSpeaker,
            self.sttxtPtCongregation, self.btnPtCongregation, self.sttxtPtTheme, self.btnPtTheme,
            self.sttxtPtHospitality, self.btnPtHospitality, self.sttxtPtAwaySpeaker, 
            self.btnPtAway1Speaker, self.sttxtPtAwayCong, self.btnPtAway1Cong,
            self.sttxtPtAwayTheme, self.btnPtAway1Theme,
            self.btnPtAway2Speaker, self.btnPtAway2Cong, self.btnPtAway2Theme,
            self.btnPtAway3Speaker, self.btnPtAway3Cong, self.btnPtAway3Theme
            ]},
            {'WT':[self.cmbbxWtSong1, self.sttxtWtSong1, self.sttxtWtSong2,
            self.cmbbxWtSong2, self.sttxtWtReader, self.btnWtReader]}, 
            {'Other':[self.btnOtLtCleaning, self.sttxtOtLtCleaning, self.sttxtOtHvCleaning,
            self.btnOtHvCleaning]}]
        for btn in allButtons:
            if meeting in btn:
                for i in btn[meeting]:
                    i.Show(True)
            else:
                for i in btn[btn.keys()[0]]:
                    i.Show(False)
        self.on_treeDates_select(self)
        if event != None: event.Skip()
    
    def on_radioSchoolAssignments_select(self, event=None):
        #todo: when switching between schools, if talk 3 has assistant, it might not show
        """ update talks based upon school selected """
        schoolTalks = [[],['1a ID', '1a StudyID','2a ID', '2a StudyID','2a Assist ID','3a ID', '3a StudyID','3a Assist ID'],['1b ID', '1b StudyID','2b ID', '2b StudyID','2b Assist ID','3b ID', '3b StudyID','3b Assist ID'],['1c ID', '1c StudyID','2c ID', '2c StudyID','2c Assist ID','3c ID', '3c StudyID','3c Assist ID']]
        btns = [self.btnTalk1, self.btnTalk1Study, self.btnTalk2, self.btnTalk2Study, self.btnTalk2Assist, self.btnTalk3, self.btnTalk3Study, self.btnTalk3Assist]
        school = int(self.radioSchoolAssignments.GetStringSelection())
        schools = ['','a','b','c']
        date = self.treeDates.GetItemText(self.treeDates.GetSelection())
        
        for btn in range(0, len(btns)):
            talk=schoolTalks[school][btn]
            if date in self.p.db.Calendar:
                if (btn==1) or (btn==3) or (btn==6):
                    btns[btn].SetLabel(str(self.p.db.Calendar[date].get(talk,'')))
                    btns[btn].SetToolTipString(Tms.studyNumber[self.p.db.Calendar[date].get(talk,0)])
                else:
                    btns[btn].SetLabel(Tms.GetStudentsName(self, self.p.db.Calendar[date].get(talk,'Click to Assign Student')))
            elif (btn==1) or (btn==3) or (btn==6): #these are study buttons
                btns[btn].SetLabel('')
                btns[btn].SetToolTipString('')
            else:
                btns[btn].SetLabel('Click to Assign Student')
        
    def showStudentTalks(self, truth):
        """ Show or hide student talks based on whether there is a written review or not"""
        self.sttxtTmsReader.Show(not truth)
        self.btnTmsReader.Show(not truth)
        self.radioSchoolAssignments.Show(truth)
        self.sttxtTalk1.Show(truth)
        self.btnTalk1.Show(truth)
        self.sttxtTalk1Study.Show(truth)
        self.btnTalk1Study.Show(truth)
        self.sttxtTalk2.Show(truth)
        self.btnTalk2.Show(truth)
        self.sttxtTalk2Study.Show(truth)
        self.btnTalk2Study.Show(truth)
        self.sttxtTalk2Assist.Show(truth)
        self.btnTalk2Assist.Show(truth)
        self.sttxtTalk3.Show(truth)
        self.btnTalk3.Show(truth)
        self.sttxtTalk3Study.Show(truth)
        self.btnTalk3Study.Show(truth)
        self.sttxtTalk3Assist.Show(truth)
        self.btnTalk3Assist.Show(truth)

    def saveTalkBtnLabel(self, button, talk):
        schools = ['','a','b','c']
        talks = {
            'Reader':['', 'Reader ID', 'Reader ID', 'Reader ID'], 
            'H':['', 'H ID', 'H ID', 'H ID'], 
            '1':['', '1a ID', '1b ID', '1c ID'], 
            '2':['', '2a ID', '2b ID', '2c ID'], 
            '2 Assist':['', '2a Assist ID', '2b Assist ID', '2c Assist ID'], 
            '3':['', '3a ID', '3b ID', '3c ID'], 
            '3 Assist':['', '3a Assist ID', '3b Assist ID', '3c Assist ID']
            }
        date = self.treeDates.GetItemText(self.treeDates.GetSelection())
        student = self.lstStudent.GetStringSelection()
        id = Tms.GetStudentId(self, student)
        school = self.radioSchoolAssignments.GetStringSelection()
        #todo: check if student is already assigned to a talk on this date before using this person
        button.SetLabel(student)
        if talk=='3':
            # if male student, disable '3 assist' button
            if self.p.db.Students[id]['Sex'] == 'Male':
                self.btnTalk3Assist.Show(False)
                self.sttxtTalk3Assist.Show(False)
            else:
                self.btnTalk3Assist.Show(True)
                self.sttxtTalk3Assist.Show(True)
        talkID = talks[talk][int(school)]

        if date in self.p.db.Calendar:
            self.p.db.Calendar[date][talkID] = id
        else:
            self.p.db.Calendar[date] = {talkID:id}  
        
    def saveStudyBtnLabel(self, button, talk):
        """ select the study that student should work on """
        Dlg = Tms.SelectStudy(self)
        try:
            Dlg.ShowModal()
        finally:
            study= Dlg.studyID
            Dlg.Destroy()        
        schools = ['','a','b','c']
        date = self.treeDates.GetItemText(self.treeDates.GetSelection())
        school = self.radioSchoolAssignments.GetStringSelection()
        button.SetLabel(str(study))
        talk = talk + schools[int(school)] + ' StudyID'
        if date in self.p.db.Calendar:
            self.p.db.Calendar[date][talk] = study
        else:
            self.p.db.Calendar[date] = {talk:study}  
        button.SetToolTipString(Tms.studyNumber[study])
        
    def on_btnTalk1_select(self, event=None):
        self.saveTalkBtnLabel(button=self.btnTalk1, talk='1')
    
    def on_btnTalk1Study_select(self, event=None):
        self.saveStudyBtnLabel(button=self.btnTalk1Study, talk='1')
        
    def on_btnTalk2_select(self, event=None):
        self.saveTalkBtnLabel(button=self.btnTalk2, talk='2')
        
    def on_btnTalk2Study_select(self, event=None):
        self.saveStudyBtnLabel(button=self.btnTalk2Study, talk='2')
        
    def on_btnTalk2Assist_select(self, event=None):
        self.saveTalkBtnLabel(button=self.btnTalk2Assist, talk='2 Assist')
        
    def on_btnTalk3_select(self, event=None):
        self.saveTalkBtnLabel(button=self.btnTalk3, talk='3')
        
    def on_btnTalk3Study_select(self, event=None):
        self.saveStudyBtnLabel(button=self.btnTalk3Study, talk='3')
        
    def on_btnTalk3Assist_select(self, event=None):
        self.saveTalkBtnLabel(button=self.btnTalk3Assist, talk='3 Assist')
        
    def on_btnTmsReader_select(self, event=None):
        self.saveTalkBtnLabel(button=self.btnTmsReader, talk='Reader')        
        
    def on_btnHighlights_select(self, event=None):
        """The Bible Highlights button has been pressed"""
        self.saveTalkBtnLabel(button=self.btnHighlights, talk='H')
        
    def on_ckbxCoVisit_change(self, event=None):
        date = self.treeDates.GetItemText(self.treeDates.GetSelection())
        if date in self.p.db.Calendar:
            self.p.db.Calendar[date]['CO Visit'] = self.ckbxCoVisit.GetValue()
        else:
            self.p.db.Calendar[date] = {'CO Visit': self.ckbxCoVisit.GetValue()}
        
    def on_cmbbxNumSchools_change(self, event=None):
        date = self.treeDates.GetItemText(self.treeDates.GetSelection())
        if date in self.p.db.Calendar:
            self.p.db.Calendar[date]['#Schools'] = self.cmbbxNumSchools.GetValue()
        else:
            self.p.db.Calendar[date] = {'#Schools': self.cmbbxNumSchools.GetValue()}
        
    def on_cmbbxMeetingStatus_change(self, event=None):
        #todo: check why meeting status always shows scheduled even when unscheduled
        date = self.treeDates.GetItemText(self.treeDates.GetSelection())
        status = self.cmbbxMeetingStatus.GetValue()
        if (status == 'Concluded') and (self.radioSchedule.GetStringSelection() == 'TMS'):
            self.btnHighlightsResults.Show(True)
            
            if (date in self.p.db.SourceMaterial) and (self.p.db.SourceMaterial[date]['Review'] == 'Y'):
                self.btnTmsReaderResults.Show(True)
                self.btnTalk1Results.Show(False)
                self.btnTalk2Results.Show(False)
                self.btnTalk3Results.Show(False)
                self.btnTalk2AssistResults.Show(False)
                self.btnTalk3AssistResults.Show(False)
            else:
                self.btnTmsReaderResults.Show(False)
                self.btnTalk1Results.Show(True)
                self.btnTalk2Results.Show(True)
                self.btnTalk3Results.Show(True)
                self.btnTalk2AssistResults.Show(True)
                # only show talk3assist if there is an assistant
                if self.btnTalk3Assist.GetLabel() == 'Click to Assign Student':
                    self.btnTalk3AssistResults.Show(False)
                else:
                    self.btnTalk3AssistResults.Show(True)
                
        else:
            self.btnHighlightsResults.Show(False)
            self.btnTalk1Results.Show(False)
            self.btnTalk2Results.Show(False)
            self.btnTalk3Results.Show(False)
            self.btnTalk2AssistResults.Show(False)
            self.btnTalk3AssistResults.Show(False)
            self.btnTmsReaderResults.Show(False)
            
        # save meeting status in calendar
        if date in self.p.db.Calendar:
            self.p.db.Calendar[date]['Status'] = self.cmbbxMeetingStatus.GetValue()
        else:
            self.p.db.Calendar[date] = {'Status': self.cmbbxMeetingStatus.GetValue()}
        
    def loadSourceMaterial(self, btn, partID, Item):
        if Item in self.p.db.SourceMaterial:
            date = self.p.db.SourceMaterial[Item]
            source = date.get(partID,'Enter Source Material')
            if source == '': source = 'Enter Source Material'
            btn.SetLabel(source)
        
    def loadBtnConductor(self, Item, btn, partID):
        if Item in self.p.db.Calendar:
            date = self.p.db.Calendar[Item]
            name = Tms.GetStudentsName(self, date.get(partID,''))
            if name != '':
                btn.SetLabel(name)
            else:
                btn.SetLabel('Click to Select Conductor')
        else:
            btn.SetLabel('Click to Select Conductor')
    def btnLoadSong(self, btn, partID, Item):
        if Item in self.p.db.SourceMaterial:
            date = self.p.db.SourceMaterial[Item]
            try:
                song = int(date.get(partID, 0))
                if song == '':
                    song = 0
            except:
                song = 0
            btn.SetSelection(song)
        
    def on_treeDates_select(self, event=None):
        #todo: when date is changed reload lstStudents window to reflect changes in talk assignments !!!
        item = self.treeDates.GetItemText(self.treeDates.GetSelection())
        meeting = self.radioSchedule.GetStringSelection()
        if meeting == 'Other':
            # load buttons for 'Other'
            loadOtherButtons(self, item)
            
        if meeting == 'WT':
            loadWtButtons(self, item)
                
        if meeting == 'PT':
            loadPtButtons(self, item)
            
        if meeting == 'SM':
            loadSmButtons(self, item)
            
        if meeting == 'CBS':
            loadCbsButtons(self, item)
                
        if meeting == 'TMS':
            loadTmsButtons(self, item)

        self.on_cmbbxMeetingStatus_change() #check if results button should be showing or not    
        
    def loadScheduleWindow(self):
        # first load 'tms dates' window
        Tms.loadTreeDates(self, self.treeDates)

        # set school to '1'
        self.radioSchoolAssignments.SetStringSelection('1')
        
    def on_Schedule_close(self, event=None):
        if event == None:
            self.Close()
        else:
            self.p.Enable(True)
            self.p.SetFocus()
            event.Skip()
        
    def on_lstStudent_select(self, event=None):
        """Student Selected, so load student info window"""
        if self.lstStudent.GetSelection()!= -1:
            id = Tms.GetStudentId(self, self.lstStudent.GetStringSelection())
            #first get talk availability
            htmlText = ''
            if id in self.p.db.Students:
                dat = self.p.db.Students[id]
                htmlText += 'Talk Availability: '
                if dat.get('Talk H', False): 
                    htmlText += 'Highlights '
                    self.btnHighlights.Enable(True)
                else:
                    self.btnHighlights.Enable(False)
                if dat.get('Talk 1', False): 
                    htmlText += 'Talk 1 '
                    self.btnTalk1.Enable(True)
                    self.btnTalk1Study.Enable(True)
                else:
                    self.btnTalk1.Enable(False)
                    self.btnTalk1Study.Enable(False)
                    
                if dat.get('Talk 2', False): 
                    htmlText += 'Talk 2 '
                    self.btnTalk2.Enable(True)
                    self.btnTalk2Study.Enable(True)
                else:
                    self.btnTalk2.Enable(False)
                    self.btnTalk2Study.Enable(False)
                    
                if dat.get('Talk 3', False): 
                    htmlText += 'Talk 3 '
                    self.btnTalk3.Enable(True)
                    self.btnTalk3Study.Enable(True)
                else:
                    self.btnTalk3.Enable(False)
                    self.btnTalk3Study.Enable(False)
                    
                if dat.get('Assist', False): 
                    htmlText += 'Assistant '
                    self.btnTalk2Assist.Enable(True)
                    self.btnTalk3Assist.Enable(True)
                else:
                    self.btnTalk2Assist.Enable(False)
                    self.btnTalk3Assist.Enable(False)
                    
                if dat.get('Reader', False): 
                    htmlText += 'Reader'
                    self.btnTmsReader.Enable(True)
                else:
                    self.btnTmsReader.Enable(False)
                    
                htmlText += '<br>'
                if dat.get('Remarks',"") !="": 
                    htmlText += '<span>Student Remarks: ' + dat.get('Remarks','') + '</span><br />'
            allTalks = []
            for date in self.p.db.Calendar:
                if self.p.db.Calendar[date].get('Status','') != 'Concluded':
                    for talk in self.talks:
                        if self.p.db.Calendar[date].get(talk,'') == id:
                            allTalks.append([Tms.convertCalendar2Number(date), date, talk.split(' ')[0], 'Scheduled', ''])
            if id in self.p.db.TalkHistory:
                for talk in self.p.db.TalkHistory[id]:
                    date = talk['Date'][1] + '/' + talk['Date'][2] + '/' + talk['Date'][0]
                    allTalks.append([Tms.convertFile2Number(talk['Date']), date, talk['Talk'], talk.get('Result', ''), talk.get('Remarks', ''), talk.get('Study','')])
            count = 0
            for talk in sorted(allTalks, reverse=True):
                htmlText += '<b>Date: </b>' + talk[1] + '     '
                htmlText += '<b>Talk: </b>' + talk[2] + '     '
                if (len(talk) >= 6) and (talk[5] != 0):
                    htmlText += '<b>Study: </b>' + str(talk[5]) + '     '
                htmlText += '<b>Result: </b>' + talk[3] + '<br />'
                if talk[4] != '':
                    htmlText += '<b>Remarks: </b>' + talk[4] + '<br />'


            # clear html window and put in text
            self.htmlStudentInfo.SetPage(self.studentInfoText + htmlText)
        if event != None: event.Skip()
        
        # enable or disable talk buttons based on students talk availability
        #self.enableTalkButtons(student)

    def On_Back_btn(self, event=None):
        """back button pressed, so close student window"""
        self.on_Schedule_close()

if __name__ == '__main__':
    Tms.Main()

